package com.haidechizi.mybatis.demo;

public class Response<T> {

    public static final Integer SUCCESS = 200;
    public static final Integer FAIL = -1;
    public static final Integer NOT_LOGIN = 0;
    public static final String SUCCESS_MESSAGE = "访问成功";
    public static final String FAIL_MESSAGE = "系统异常";

    private Integer code;
    private String message;
    private T data;

    private Response(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private Response(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static final ResponseBuilder builder() {
        return new ResponseBuilder();
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public static class ResponseBuilder<T> {

        private Integer code = SUCCESS;
        private String message = SUCCESS_MESSAGE;
        private T data;

        private ResponseBuilder() {

        }

        public ResponseBuilder code(Integer code) {
            this.code = code;
            return this;
        }

        public ResponseBuilder message(String message) {
            this.message = message;
            return this;
        }

        public ResponseBuilder data(T data) {
            this.data = data;
            return this;
        }

        public Response build() {
            return new Response(this.code, this.message, this.data);
        }

        public Response success() {
            return success(SUCCESS_MESSAGE);
        }

        public Response success(String message) {
            return new Response(SUCCESS, message);
        }

        public Response fail() {
            return fail(FAIL_MESSAGE);
        }

        public Response fail(String message) {
            return new Response(FAIL, message);
        }
    }
}
